package cate.game.activity.worldboss;

import cate.common.table.activity.worldboss.WorldBossBaseRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDFunc;
import cate.game.activity.base.core.RoleFarmActivitySingle;
import cate.game.activity.worldboss.msg.WorldBossDataResp;
import cate.game.play.part.FightSide;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import cate.game.role.farm.PlayTimes;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@NoteClass("玩家单个世界boss活动数据")
public class RoleWorldBoss extends RoleFarmActivitySingle {

	@NoteField("副本购买次数")
	public PlayTimes playTimes;

	@Override
	public void initialize(Role role) {
		super.initialize(role);
		if (playTimes == null) {
			playTimes = new PlayTimes();
		}
		playTimes.initialize();
	}


	@Override
	public void onDaySpan(boolean silence) {
		super.onDaySpan(silence);
		playTimes.reset();
		if(!silence){
			noticeUpdate();
		}
	}

	@Override
	public void onRoleOpen() {
	}

	@Override
	public void onRoleEnd() {
		fighting = false;
		playTimes.resetAll();
	}

	@Override
	public void initSend() {
		noticeUpdate();
	}

	@Override
	public void excelCheck() {

	}

	public void noticeUpdate() {
		role.sendNow(new WorldBossDataResp(this));
	}

	@Override
	public int getFightFuncId() {
		return base.funcId;
	}

	public EcResult<Void> challenge() {
		EcResult<Void> r = new EcResult<>();
		if (isFighting()) {
			return r.fail("当前战斗未结束");
		}
		WorldBossBaseRow row = getRole().getGame().table.worldBoss.base.get(base.configTid);
		if (row == null) {
			return r.fail("配置不存在!");
		}
		r = playTimes.available(role, getFightFuncId());
		if (!r.ok()) {
			return r;
		}
		EcResult<FightSide> fsRes = role.getGame().fight.side.createFightSideByCode(GDFight.Team.B, row.dynInsCode);
		if (!fsRes.ok()) {
			return r.fail(fsRes.message);
		}
		playTimes.added();
		WorldBossFTC context = new WorldBossFTC(role,funcId, row);
		context.funcId = getFightFuncId();
		context.setPositionApply(GDFunc.ADVENTURE);
		EcResult<PlayPO> fightRes = role.getGame().play.startByRole(role, context, fsRes.data);
		if (!fightRes.ok()) {
			return r.fail(fightRes.message);
		}
		this.startFighting();
		noticeUpdate();
		return r.success();
	}
}
